package com.xopencode.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;

import java.io.IOException;
import java.util.Set;

/**
 * @Auther: Mcb
 * @Date: 2022/3/2 23:39
 * @Description: 元数据读取器
 */
public class Metadata {

    public static void main(String[] args) throws IOException {

        ApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        SimpleMetadataReaderFactory simpleMetadataReaderFactory = new SimpleMetadataReaderFactory();

        Resource resource = applicationContext.getResource("file://D:\\idea\\Storage\\src\\main\\java\\com\\xopencode\\spring\\config\\SpringListenerConfig.java");

        MetadataReader metadataReader = simpleMetadataReaderFactory.getMetadataReader("com.xopencode.spring.config.SpringListenerConfig");

        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        String className = classMetadata.getClassName();
        System.out.println(className);

        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        for (String annotationType : annotationMetadata.getAnnotationTypes()) {
            System.out.println(annotationType);
        }

        //查找添加了EventListener注解的方法
        Set<MethodMetadata> annotatedMethods = annotationMetadata.getAnnotatedMethods(EventListener.class.getName());
        for (MethodMetadata annotatedMethod : annotatedMethods) {
            System.out.println(annotatedMethod.getMethodName());
        }


    }
}
